// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.dbmanager.ui.action;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.commons.utils.UIUtils;
import org.dengues.core.CorePlugin;
import org.dengues.core.metadata.MetadataTable;
import org.dengues.warehouse.dbmanager.ui.AbstractEditorComposite;
import org.dengues.warehouse.dbmanager.ui.DatabaseManagerDialog;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Display;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class CleanAllAction extends AbstractToolAction {

    private final AbstractEditorComposite sqlEditorComposite;

    /**
     * Qiang.Zhang.Adolf@gmail.com CleanAllAction constructor comment.
     * 
     * @param sqlEditorComposite
     * @param id
     */
    public CleanAllAction(AbstractEditorComposite sqlEditorComposite, int id) {
        super(Messages.getString("CleanAllAction.clean.allData")); //$NON-NLS-1$
        setImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.DBINFO_CLEAN));
        this.sqlEditorComposite = sqlEditorComposite;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.action.Action#run()
     */
    @Override
    public void run() {
        final DatabaseManagerDialog dialog = sqlEditorComposite.getDialog();
        IRunnableWithProgress progress = new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
                try {
                    final Connection conn = CorePlugin.getDefault().getDesignerCoreService().getCurrentConnection();
                    conn.setAutoCommit(false);
                    Display.getDefault().asyncExec(new Runnable() {

                        public void run() {
                            Object fe = ((StructuredSelection) dialog.getDatabaseNavigateTree().getViewer().getSelection())
                                    .getFirstElement();
                            if (fe instanceof MetadataTable) {
                                MetadataTable table = (MetadataTable) fe;
                                String tableName = table.getTableName();
                                try {
                                    Statement stmt = conn.createStatement();
                                    stmt.execute("delete from " + tableName); //$NON-NLS-1$
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                }
                                sqlEditorComposite.getSqlRunAction().setEnabled(true);
                                sqlEditorComposite.getCancelAction().setEnabled(true);
                            }
                        }

                    });
                } catch (CoreException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    monitor.done();
                }

            }
        };
        UIUtils.runWithProgress(progress, true, dialog.getProgressMonitor(), dialog.getShell());
    }
}
